﻿using Fuse8.ContentManagementFramework.Administration.Attributes;
using Fuse8.ContentManagementFramework.BizFacade;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;

namespace Fuse8.ContentManagementFramework.Administration.Models
{
    [Serializable]
    public class CmsAdminTemplateModel : CmsSiteAwareModel
    {
        [GlobalizedRequired("Please enter name", "cms_Templates")]
        public string TemplateName { get; set; }

        [GlobalizedRequired("Please enter template url", "cms_Templates")]
        public string TemplateUrl { get; set; }

        public bool IsDefault { get; set; }

        public int TemplateTypeKey { get; set; }

        public IEnumerable<dynamic> TemplateTypeList { get; set; }

        public IEnumerable<string> TemplateRegions { get; set; }

        public int? DefaultRegionId { get; set; }

        public CmsTemplateTypeKind TemplateType
        {
            get
            {
                return (CmsTemplateTypeKind)Enum.ToObject(
                    typeof(CmsTemplateTypeKind),
                    TemplateTypeKey);
            }
        }

        public IEnumerable<SelectListItem> Regions
        {
            get
            {
                //todo create command to get olnly needed regions
                return new BusinessServiceFacade<CmsTemplateRegion, int>().FindAll().Where(p => p.TemplateId == Id).Select(
                    p => new SelectListItem() { Text = p.TemplateRegionName, Value = p.Id.ToString() });
            }
        }

        public CmsAdminTemplateModel()
        { }

        public CmsAdminTemplateModel(CmsTemplate template)
        {
            Guard.ArgumentNotNull(template, "template");

            Id = template.Id;
            TemplateName = template.TemplateName;
            TemplateUrl = template.TemplateUrl;
            IsDefault = template.IsDefault;
            TemplateTypeKey = 3;
            SiteGroupId = template.SiteGroupId;
            SiteId = template.SiteId;
            DefaultRegionId = template.DefaultRegionId;
        }

        public static implicit operator CmsTemplate(CmsAdminTemplateModel model)
        {
            Guard.ArgumentNotNull(model, "model");

            var result = new CmsTemplate
            {
                Id = model.Id,
                TemplateName = model.TemplateName,
                TemplateUrl = model.TemplateUrl,
                IsDefault = model.IsDefault,
                TemplateTypeKey = model.TemplateTypeKey,
                SiteGroupId = model.SiteGroupId,
                SiteId = model.SiteId,
                DefaultRegionId = model.DefaultRegionId
            };

            SiteAwareEntityObjectMediator.SetSiteEntityByCategory(result, model.SiteCategory);

            if ((model.TemplateRegions != null) && model.TemplateRegions.Any())
            {
                result.TemplateRegions =
                    model.TemplateRegions
                    .Select(p =>
                        new CmsTemplateRegion
                        {
                            TemplateRegionName = p
                        });
            }

            return result;
        }
    }
}